package com.css.fxfzypg.modules.auxiliaryDecisionManage.paramSetting.repository.impl;

import com.css.fxfzypg.modules.auxiliaryDecisionManage.paramSetting.entity.ParamSetting;
import com.css.fxfzypg.modules.auxiliaryDecisionManage.paramSetting.entity.ParamSettingVO;
import com.css.fxfzypg.modules.auxiliaryDecisionManage.paramSetting.repository.ParamSettingRepository;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/03/22
 * @Description: 设置参数
 */
@Repository
public class ParamSettingRepositoryImpl implements ParamSettingRepository {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public ParamSettingVO getParamSetting() {
        String sql = "select * from ypg_param_setting ";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(ParamSettingVO.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public void save(ParamSetting paramSetting) {
        String sql = "insert into ypg_param_setting (id,O,U,W1,W2,D1,D2,W3) " +
                "values(:id,:O,:U,:W1,:W2,:D1,:D2,:W3)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(paramSetting);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void delete() {
        String sql = "delete from ypg_param_setting";
        jdbcTemplate.update(sql);
    }
}
